#include "HTTPRequest.hpp"
#include "sonic/sonic.h"
#include "spdlog/spdlog.h"
#include "utils/utils.hpp"
#include <cstring>
#include <fstream>
#include <iostream>
#include <string>
static std::string send_url = "message_queue_enqueue";

static std::string get_url = "message_latest_message";
const std::string config_path = "../config/config.json";

void init_config(const std::string &path, std::string &send_url,
                 std::string &get_url);

int main(int argc, char *argv[]) {
  spdlog::info("argc is {}", argc);
  init_config(config_path, send_url, get_url);
  // spdlog::info("send: {}", argv[2]);
  if (strcmp(argv[1], "-h")==0 || strcmp(argv[1], "-help") == 0) {
    Utils::usage();
    return 0;
  }
  if (strcmp(argv[1], "-send") == 0 || strcmp(argv[1], "-s") == 0) {
    try {
      spdlog::info("in send: ");
      auto response = Utils::send_message(argv[2], send_url);
      if (response.status.code != http::Status::Ok) {
        spdlog::info("Request failed with status code {}",
                     response.status.code);
        return -1;
      }
      Utils::proc_resp_send_message(response);
    } catch (const std::exception &e) {
      std::cerr << "Request failed, error: " << e.what() << '\n';
    }
  } else if (strcmp(argv[1], "-get") == 0 || strcmp(argv[1], "-g") == 0) {
    auto number = atoi(argv[2]);
    spdlog::info("get: {}", number);
    try {
      auto response = Utils::get_message(get_url, number);
      if (response.status.code != http::Status::Ok) {
        spdlog::info("Request failed with status code {}",
                     response.status.code);
        return -1;
      }
      Utils::proc_resp_get_message(response);
    } catch (const std::exception &e) {
      std::cerr << "Request failed, error: " << e.what() << '\n';
    }
  }else if (strcmp(argv[1], "-update")==0||strcmp(argv[1], "-u")==0) {
    spdlog::info("update: {} {}", argv[2], argv[3]);
    const std::string key = argv[2];
    const std::string value = argv[3];
    Utils::config_write(config_path, key, value);
  }
  return 0;
}
void init_config(const std::string &path, std::string &send_url,
                        std::string &get_url) {
  std::ifstream file(path);
  if (!file.is_open()) {
    spdlog::info("file {} not exist", path);
  }
  std::string data;
  std::string line;
  while (std::getline(file, line)) {
    data += line;
  }
  file.close();
  sonic_json::Document doc;
  doc.Parse(data);
  if (doc.HasParseError()) {
    spdlog::info("Parse failed");
  }
  // Find member by key
  if (!doc.IsObject()) { // Check JSON value type.
    spdlog::info("Incorrect doc type");
  }
  auto m = doc.FindMember("base_url");
  std::string base_url = "";
  if (m != doc.MemberEnd()) {
    if (m->value.IsString()) {
      base_url = m->value.GetString();
    }
  }
  spdlog::info("base_url: {}", base_url);
  m = doc.FindMember("send_url");
  if (m != doc.MemberEnd()) {
    if (m->value.IsString()) {
      send_url = base_url + m->value.GetString();
    }
  }
  spdlog::info("send_url: {}", send_url);
  m = doc.FindMember("get_url");
  if (m != doc.MemberEnd()) {
    if (m->value.IsString()) {
      get_url = base_url + m->value.GetString();
    }
  }
  spdlog::info("get_url: {}", get_url);
}